using System;
using System.Collections.Generic;
using System.Drawing;
using GTA;
using NativeUI;

namespace CustomPeds.Menus;

public class BadgeSubMenu : Script
{
    public static UIMenu menu = new UIMenu("", TextSetting.bgs_Menu, new Point(0, -107));
    private static List<dynamic> collectionList = new List<object>(new string[1] { string.Empty });
    private static List<dynamic> overlayView = new List<object>(new string[1] { string.Empty });
    private static List<dynamic> overlayList = new List<object>(new string[1] { string.Empty });
    private static UIMenuListItem collectionItem;
    private static UIMenuListItem overlayItem;
    private static int zid;
    private static int gid;

    public BadgeSubMenu()
    {
        Main.pool.Add(menu);
        menu.OnListChange += OnListChange;
        menu.OnItemSelect += OnItemSelect;
        menu.OnMenuClose += OnMenuClose;
    }

    public static void InitBadgeSubMenu(int _zid, int _gid)
    {
        zid = _zid;
        gid = _gid;
        menu.Clear();
        collectionList = new List<object>();
        overlayView = new List<object>();
        overlayList = new List<object>();
        List<Decoration.Collection> list;
        Skin.Badges badges;
        if (DressingScript.Dressing == Main.Member.P2)
        {
            if (Main.P2.skin.BadgeData == null) Main.P2.skin.GetDecorationData(Decoration.Type.TYPE_BADGE);
            list = Main.P2.skin.BadgeData[zid];
            badges = Main.P2.skin.Badge[zid][gid];
        }
        else
        {
            if (Main.P1.skin.BadgeData == null)
            {
                Main.P1.skin.GetDecorationData(Decoration.Type.TYPE_BADGE);
                Main.Clone.skin.GetDecorationData(Decoration.Type.TYPE_BADGE);
            }
            list = Main.P1.skin.BadgeData[zid];
            badges = Main.P1.skin.Badge[zid][gid];
        }
        foreach (var item in list) collectionList.Add(item.nameHash);
        int cIndex = collectionList.FindIndex(name => name == badges.collectionName);
        if (cIndex == -1) cIndex = 0;
        foreach (var ov in list[cIndex].overlay)
        {
            string display = !string.IsNullOrEmpty(ov.award) ? ov.award : (!string.IsNullOrEmpty(ov.txtHash) ? ov.txtHash : (ov.nameHash ?? string.Empty));
            overlayView.Add(display);
            overlayList.Add(ov.nameHash);
        }
        int oIndex = overlayList.FindIndex(name => name == badges.overlayName);
        if (oIndex == -1) oIndex = 0;
        collectionItem = new UIMenuListItem(TextSetting.tbs_ListItem_Collection, collectionList, cIndex);
        overlayItem = new UIMenuListItem(TextSetting.tbs_ListItem_Overlay, overlayView, oIndex);
        menu.SetBannerType(new UIResRectangle());
        menu.AddItem(collectionItem);
        menu.AddItem(overlayItem);
        menu.AddItem(new UIMenuItem(TextSetting.generic_MenuItem_Randomize));
        menu.AddItem(new UIMenuItem(TextSetting.generic_MenuItem_Clear));
        menu.AddItem(new UIMenuItem(TextSetting.generic_MenuItem_Distance));
        menu.AddItem(new UIMenuItem(TextSetting.generic_MenuItem_TurnLeft));
        menu.AddItem(new UIMenuItem(TextSetting.generic_MenuItem_TurnRight));
        menu.AddItem(new UIMenuItem(TextSetting.generic_MenuItem_AngleUp));
        menu.AddItem(new UIMenuItem(TextSetting.generic_MenuItem_AngleDown));
        menu.RefreshIndex();
        menu.Visible = true;
    }

    public static void UpdateBadgeValue(bool isMenuExit = false)
    {
        if (DressingScript.Dressing == Main.Member.P2)
        {
            if (!isMenuExit || Main.P2.skin.Badge[zid][gid].action)
            {
                Main.P2.skin.Badge[zid][gid].collectionName = collectionList[collectionItem.Index];
                Main.P2.skin.Badge[zid][gid].overlayName = overlayList[overlayItem.Index];
            }
        }
        else if (!isMenuExit || Main.P1.skin.Badge[zid][gid].action)
        {
            Main.P1.skin.Badge[zid][gid].collectionName = collectionList[collectionItem.Index];
            Main.P1.skin.Badge[zid][gid].overlayName = overlayList[overlayItem.Index];
            Main.Clone.skin.Badge[zid][gid].collectionName = collectionList[collectionItem.Index];
            Main.Clone.skin.Badge[zid][gid].overlayName = overlayList[overlayItem.Index];
        }
    }

    private static void OnListChange(UIMenu sender, UIMenuListItem list, int index)
    {
        if (DressingScript.Dressing == Main.Member.P2)
        {
            if (list.Text == sender.MenuItems[0].Text)
            {
                Main.P2.skin.ClearDecoration(Decoration.Type.TYPE_BADGE, gid, (Decoration.Zone)zid);
                SkinHelper.SetPedDecoration(Main.P2.Character, collectionList[collectionItem.Index], overlayList[0]);
                UpdateBadgeValue();
                InitBadgeSubMenu(zid, gid);
            }
            Main.P2.skin.ClearDecoration(Decoration.Type.TYPE_BADGE, gid, (Decoration.Zone)zid);
            SkinHelper.SetPedDecoration(Main.P2.Character, collectionList[collectionItem.Index], overlayList[overlayItem.Index]);
            Main.P2.skin.Badge[zid][gid].action = true;
            return;
        }
        if (list.Text == sender.MenuItems[0].Text)
        {
            Main.P1.skin.ClearDecoration(Decoration.Type.TYPE_BADGE, gid, (Decoration.Zone)zid);
            Main.Clone.skin.ClearDecoration(Decoration.Type.TYPE_BADGE, gid, (Decoration.Zone)zid);
            SkinHelper.SetPedDecoration(Main.P1.Character, collectionList[collectionItem.Index], overlayList[0]);
            SkinHelper.SetPedDecoration(Main.Clone.Character, collectionList[collectionItem.Index], overlayList[0]);
            UpdateBadgeValue();
            InitBadgeSubMenu(zid, gid);
        }
        Main.P1.skin.ClearDecoration(Decoration.Type.TYPE_BADGE, gid, (Decoration.Zone)zid);
        Main.Clone.skin.ClearDecoration(Decoration.Type.TYPE_BADGE, gid, (Decoration.Zone)zid);
        SkinHelper.SetPedDecoration(Main.P1.Character, collectionList[collectionItem.Index], overlayList[overlayItem.Index]);
        SkinHelper.SetPedDecoration(Main.Clone.Character, collectionList[collectionItem.Index], overlayList[overlayItem.Index]);
        Main.P1.skin.Badge[zid][gid].action = true;
        Main.Clone.skin.Badge[zid][gid].action = true;
    }

    private static void OnItemSelect(UIMenu sender, UIMenuItem item, int index)
    {
        switch (index)
        {
            case 2:
                {
                    Random random = new Random(Guid.NewGuid().GetHashCode());
                    collectionItem.Index = random.Next(0, collectionList.Count);
                    UpdateBadgeValue();
                    InitBadgeSubMenu(zid, gid);
                    overlayItem.Index = random.Next(0, overlayList.Count);
                    if (DressingScript.Dressing == Main.Member.P2)
                    {
                        Main.P2.skin.ClearDecoration(Decoration.Type.TYPE_BADGE, gid, (Decoration.Zone)zid);
                        SkinHelper.SetPedDecoration(Main.P2.Character, collectionList[collectionItem.Index], overlayList[overlayItem.Index]);
                        Main.P2.skin.Badge[zid][gid].action = true;
                    }
                    else
                    {
                        Main.P1.skin.ClearDecoration(Decoration.Type.TYPE_BADGE, gid, (Decoration.Zone)zid);
                        Main.Clone.skin.ClearDecoration(Decoration.Type.TYPE_BADGE, gid, (Decoration.Zone)zid);
                        SkinHelper.SetPedDecoration(Main.P1.Character, collectionList[collectionItem.Index], overlayList[overlayItem.Index]);
                        SkinHelper.SetPedDecoration(Main.Clone.Character, collectionList[collectionItem.Index], overlayList[overlayItem.Index]);
                        Main.P1.skin.Badge[zid][gid].action = true;
                        Main.Clone.skin.Badge[zid][gid].action = true;
                    }
                    sender.CurrentSelection = 2;
                    break;
                }
            case 3:
                {
                    collectionItem.Index = 0;
                    overlayItem.Index = 0;
                    if (DressingScript.Dressing == Main.Member.P2)
                    {
                        Main.P2.skin.ClearDecoration(Decoration.Type.TYPE_BADGE, gid, (Decoration.Zone)zid);
                        Main.P2.skin.Badge[zid][gid].collectionName = string.Empty;
                        Main.P2.skin.Badge[zid][gid].overlayName = string.Empty;
                        Main.P2.skin.Badge[zid][gid].action = false;
                    }
                    else
                    {
                        Main.Clone.skin.ClearDecoration(Decoration.Type.TYPE_BADGE, gid, (Decoration.Zone)zid);
                        Main.P1.skin.ClearDecoration(Decoration.Type.TYPE_BADGE, gid, (Decoration.Zone)zid);
                        Main.P1.skin.Badge[zid][gid].collectionName = string.Empty;
                        Main.P1.skin.Badge[zid][gid].overlayName = string.Empty;
                        Main.Clone.skin.Badge[zid][gid].collectionName = string.Empty;
                        Main.Clone.skin.Badge[zid][gid].overlayName = string.Empty;
                        Main.P1.skin.Badge[zid][gid].action = false;
                        Main.Clone.skin.Badge[zid][gid].action = false;
                    }
                    InitBadgeSubMenu(zid, gid);
                    break;
                }
            case 4:
                DressingScript.Distance();
                break;
            case 5:
                DressingScript.TurnLeft();
                break;
            case 6:
                DressingScript.TurnRight();
                break;
            case 7:
                DressingScript.AngleUp();
                break;
            case 8:
                DressingScript.AngleDown();
                break;
        }
    }

    private static void OnMenuClose(UIMenu sender)
    {
        UpdateBadgeValue(true);
        menu.Visible = false;
        menu.RefreshIndex();
        // 不自动打开上级，避免退格时循环打开导致卡死
    }
}


